package com.wyg.blog.controller.view_blog;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wyg.blog.model.blog.Message;
import com.wyg.blog.model.blog.MyComment;
import com.wyg.blog.service.blog.MailService;
import com.wyg.blog.service.blog.MessageService;
import com.wyg.blog.utils.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.mail.MessagingException;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @description:
 * @author: WYG
 * @time: 2020/7/6 21:57
 */
@RestController
@RequestMapping("/api/view/message")
public class ViewMessageController {
    @Autowired
    MessageService messageService;

    @Autowired
    MailService mailService;

    @PostMapping("/publishMessage")
    @CrossOrigin
    public JsonResult<Integer> publishMessage(@RequestBody Map<String,String> messageInfo) throws MessagingException {
        Message message = new Message();
        message.setId(0);
        if (isMail(messageInfo.get("email"))) {
            message.setEmail(messageInfo.get("email"));
        } else {
            message.setEmail("#");
        }
        message.setAuthor(messageInfo.get("author"));
        if (messageInfo.get("author").equals("博主")) {
            message.setAvatar("https://wygandwdn.cn/NM76dl9368YZUR.png");
        } else {
            message.setAvatar(getRandomAvatar());
        }
        message.setCreate_time(new Date());
        message.setContent(messageInfo.get("content"));
        if(messageInfo.get("parent_id").equals("")){
            message.setParent_id(null);
        } else {
            message.setParent_id(Integer.parseInt(messageInfo.get("parent_id")));
        }
        message.setState(true);
        if(messageInfo.get("reply").equals("")){
            message.setReply(null);
        }else {
            message.setReply(messageInfo.get("reply"));
        }
        message.setIpAddress(messageInfo.get("ip"));
        message.setUrl(messageInfo.get("url"));
        boolean res = messageService.insertMessage(message);
        if (messageInfo.get("parent_id").equals("")) {
            String content = "风在哪您好,您的个人博客收到一条留言板评论,<a style=\"text-decoration:none\" href=\"https://www.wygandwdn.cn/message\">点击前往查看</a>" +
                    ",感谢捧场!<br><div><p>祝您生活愉快,万事如意!</p></div>";
            mailService.sendMessage("1973017714@qq.com", content);
        } else {
            String email = messageService.selectEmail(Integer.parseInt(messageInfo.get("parent_id")));
            if (isMail(email)) {
                String content = "您在风在哪个人博客留言板的评论收到回复,<a style=\"text-decoration:none\" href=\"https://www.wygandwdn.cn/message\">点击前往查看</a>" +
                        ",感谢捧场!<br><div><p>祝您生活愉快,万事如意!</p></div>";
                mailService.sendMessage(email, content);
            }
        }
        if(res){
            return new JsonResult<>(1,"200","留言成功!");
        }else {
            return new JsonResult<>(0,"1103","留言失败!");
        }
    }

    @GetMapping("/getMyMessages")
    @CrossOrigin
    public JsonResult<List<MyComment<Message>>> getMyMessage(@RequestParam("current")Integer current,
                                                             @RequestParam("size")Integer size){
        Page<?> page = new Page<>(current, size);
        List<MyComment<Message>> messages = messageService.selectMessagesByParentId(page);
        System.out.println(messages);
        return new JsonResult<>(messages, "200", "留言查询成功!");
    }

    public String getRandomAvatar(){
        List<String> avatarList = new ArrayList<>();
        //添加自己的图片对应地址即可
        avatarList.add("https://gitee.com/blog-map-bed/first-bed/raw/master/img/typora-icon.png");
        Random random = new Random();
        int i = random.nextInt(avatarList.size());
        return avatarList.get(i);
    }

    public static boolean isMail(String str) {
        String regEx = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
        Pattern p;
        Matcher m;
        p = Pattern.compile(regEx);
        m = p.matcher(str);
        if(m.matches()) {
            return true;
        }
        return false;
    }

}
